﻿using System;
using System.Linq;
using System.Collections.Generic;

namespace Caramel.Web.Mvc.Validation.Conventions
{
	public class NumericPropertyConvention : IModelValidatorPropertyConvention
	{
		public void Apply(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName, PropertyModelValidator instance)
		{
			instance.Numeric();
		}

		public bool Accept(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
		{
			return
				new[]
				{
					typeof(byte), 
					typeof(sbyte),
					typeof(short), 
					typeof(ushort),
					typeof(int),
					typeof(uint),
					typeof(long),
					typeof(ulong),
					typeof(float), 
					typeof(double),
					typeof(decimal)
				}.
				Contains(
					Nullable.GetUnderlyingType(modelType) ?? modelType
				);
		}
	}
}
